<template>
  <el-dialog :close-on-click-modal='false' v-drag :title="$t('添加')" :v-if="dialogVisible" :visible.sync="dialogVisible" width="600px" top="10vh" @close='close'>
    <div class="dialogContentBoxMaxHeight80">
      <el-form ref="form" :model="record" label-width="110px" :rules="rules" label-position="left" style="margin:0 30px;">
        <el-form-item :label="$t('名称')" prop="name">
          <el-input v-emoji v-model="record.name" ref="recordName" size='small'></el-input>
        </el-form-item>
        <el-form-item :label="$t('本端地址')" prop="local">
          <el-input v-emoji v-model="record.local" ref="local" size='small'></el-input>
        </el-form-item>
        <el-form-item :label="$t('提示')">
          <span>{{$t('6to4的网络通信,主机IPv6地址格式:2002:ipv4_addr开头，后边根据32位全局的IPv4地址,构成6to4的48前缀,剩余16位作为子网，64位作为子网内的主机地址。')}}</span>
        </el-form-item>
      </el-form>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submit" class="addSubmit">{{$t('确 定')}}</el-button>
      <el-button @click="close" class="addClose">{{$t('取 消')}}</el-button>
    </span>
  </el-dialog>
</template>
<script>
import { add6to4 } from '@/api/network/tunnel/tunnel'
import { inputClass } from '@/utils/input-class.js'
export default {
  name: '6to4Dialog',
  data () {
    var validateName = (rule, value, callback) => {
      if (String(value).trim() === '') {
        callback(new Error(this.$t('不能为空')))
      } else {
        callback()
      }
    }
    return {
      dialogVisible: false,
      record: {
        name: '',
        local: ''
      },
      rules: {
        name: [{ required: false, validator: validateName, trigger: 'blur' }],
        local: [{ required: false, validator: validateName, trigger: 'blur' }]
      }
    }
  },
  methods: {
    init () {
      this.dialogVisible = true
      this.$nextTick(() => {
        const data = ['recordName', 'local']
        inputClass(this.$refs, data)
      })
    },
    submit () {
      this.$refs.form.validate((valid) => {
        if (valid) {
          add6to4(this.record).then(res => {
            if (res.status === 0) {
              this.$message({
                message: this.$t('添加成功'),
                type: 'success'
              })
              this.close()
              this.$parent.listData()
            }
          })
        }
      })
    },
    close () {
      this.record.name = ''
      this.record.local = ''
      this.dialogVisible = false
      this.$refs.form.resetFields()
    }
  }
}
</script>
